//
//  HTCancleWayBillView.m
//  TruckUser
//
//  Created by Ting Han on 2017/10/31.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTCancleWayBillView.h"
#import "UIView+Layout.h"
#import "UIImageView+WebCache.h"

#define boardHeight 370

@interface HTCancleWayBillView()<HTCancleItemDelegate>
@property (nonatomic,strong) HTCancleWayBillMain *cancleWaybillMain;
@property (nonatomic,strong) UIView *backView;
@property (nonatomic,weak) HTCancelItem *selectedItem;
@end

@implementation HTCancleWayBillView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = [UIScreen mainScreen].bounds;
        [self setupView];
    }
    return self;
}
- (void)setupView{
    self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
    self.backView = [[UIView alloc]initWithFrame:self.frame];
    self.backView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.1];
    [self addSubview:self.backView];
    [self addSubview:self.cancleWaybillMain];
    [self.cancleWaybillMain.cancleButton addTarget:self action:@selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
    [self.cancleWaybillMain.conformButton addTarget:self action:@selector(didClickConform) forControlEvents:UIControlEventTouchUpInside];
    self.cancleWaybillMain.fifthItem.delegate = self;
    self.cancleWaybillMain.secondItem.delegate = self;
    self.cancleWaybillMain.thirdItem.delegate = self;
    self.cancleWaybillMain.fourthItem.delegate = self;
    self.cancleWaybillMain.firstItem.delegate = self;
}
- (void)didClickItem:(HTCancelItem *)cancleItem{
    if (self.selectedItem == nil) {//没有选择的时候
        self.selectedItem = cancleItem;
        cancleItem.isSelected = !cancleItem.isSelected;
        return;
    }
    if (cancleItem == self.selectedItem) {//选择了同一个
        cancleItem.isSelected = !cancleItem.isSelected;
        self.selectedItem = nil;
        return;
    }
    //选择了不同的
    self.selectedItem.isSelected = NO;
    cancleItem.isSelected = YES;
    self.selectedItem = cancleItem;
}

- (HTCancleWayBillMain *)cancleWaybillMain{
    if (!_cancleWaybillMain) {
        _cancleWaybillMain = [[NSBundle mainBundle]loadNibNamed:@"HTCancleWayBillMain" owner:self options:nil].firstObject;
        _cancleWaybillMain.width = kHTScreenWidth/8*7;
        _cancleWaybillMain.center = self.center;
        
    }
    return _cancleWaybillMain;
}
- (void)didClickConform{
    if (self.selectedItem == nil) {
        [HTShowUtil showMessage:@"请选择取消交易原因"];
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(didCommitWithReason:cacleWaybill:)]) {
        [self.delegate didCommitWithReason:[NSString stringWithFormat:@"%ld",self.selectedItem.index]cacleWaybill:self];
    }
    
}
- (void)setupHeadImage:(NSString *)url{
    [self.cancleWaybillMain.headImageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"bg_headsculpture"]options:SDWebImageRefreshCached];
}
- (void)setupName:(NSString *)name{
    
    self.cancleWaybillMain.nameLabel.text = name;
    
}
- (void)setupCarid:(NSString *)carid{
    
    self.cancleWaybillMain.caridLabel.text = carid;
    
}
- (void)setupPhone:(NSString *)phone{
    
    self.cancleWaybillMain.phoneLabel.text = phone;
    
}
-(void)showView{
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:.3 animations:^{
        [self layoutIfNeeded];
    }];
}
-(void)dismissView{
    
    [UIView animateWithDuration:.3 animations:^{
        
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self removeFromSuperview];
    }];
}

@end
